<?php
session_start();
error_reporting (E_ERROR);
require_once ("./inc/activity.php");

if ( $_SESSION["ff"]["is_logged"] != "true" ) {
	if ( strpos($_SERVER["PHP_SELF"], "login.php") === false )
		header ( "Location: login.php" );
} else {
	/*
	 * Core engine
	 */
	$config_file = $_SESSION["FF"]["user_code"] . ".xml";
	if ( is_file("./module/test/" . $config_file)) {
		$xml = new DOMDocument('1.0', 'utf-8');
		$xml->preserveWhiteSpace = false; 
		$xml->formatOutput = true;
		$xml->load( "./module/test/".$config_file);
		$xpath = new DOMXpath($xml);
		
		/*
		 * DATABASE
		 */
		$db_conf = $xpath->query ("//database")->item(0);
		
		$dbc = mysql_connect( 	$xpath->query ("//host", $db_conf)->item(0)->nodeValue,
								$xpath->query ("//user", $db_conf)->item(0)->nodeValue,
								$xpath->query ("//password", $db_conf)->item(0)->nodeValue);
		if ( $conn = mysql_selectdb ( $xpath->query ("//name", $db_conf)->item(0)->nodeValue ) ) {
			$db = new stdClass ();
			$db->conn = $conn;
			$db->name = $xpath->query ("//name", $db_conf)->item(0)->nodeValue;
			$db->tables = Array();
			$tables = mysql_list_tables( $db->name );
			for ( $i = 0; $i< mysql_numrows ($tables); $i++ ) {
				array_push($db->tables, mysql_tablename ( $tables, $i));
			}
		} else 
			$db = false;
	}
}


class core {
	
	function core() {
		global $db; 
		global $xml; 
		global $xpath; 
		
		$this->is_logged = false;
		$this->db = $db;
		$this->xml = $xml;
		$this->xpath = $xpath;
	}
	
	function get_user_tables()
	{
		$ret = array();
		foreach ( $this->xpath->query ("//tables/table") as $table ) {
			if ( $table->getAttribute("permission") == "everybody" )
				array_push($ret, $table);
		}
		return $ret;
	}
	
	function throw_error( $error )
	{
		print ("ERRORE: " . $error);
	}
	
}

$core = new core();
?>